@@ -20,6 +20,10 @@ module Agents |
||
20 | 20 |
|
21 | 21 |
The resulting event will contain the `command` which was executed, the `path` it was executed under, the `exit_status` of the command, the `errors`, and the actual `output`. ShellCommandAgent will not log an error if the result implies that something went wrong. |
22 | 22 |
|
23 |
+ If `suppress_on_failure` is set to true, no event is emitted when `exit_status` is not zero. |
|
24 |
+ |
|
25 |
+ If `suppress_on_empty_output` is set to true, no event is emitted when `output` is empty. |
|
26 |
+ |
|
23 | 27 |
*Warning*: This type of Agent runs arbitrary commands on your system, #{Agents::ShellCommandAgent.should_run? ? "but is **currently enabled**" : "and is **currently disabled**"}. |
24 | 28 |
Only enable this Agent if you trust everyone using your Huginn installation. |
25 | 29 |
You can enable this Agent in your .env file by setting `ENABLE_INSECURE_AGENTS` to `true`. |
@@ -31,7 +35,7 @@ module Agents |
||
31 | 35 |
{ |
32 | 36 |
"command": "pwd", |
33 | 37 |
"path": "/home/Huginn", |
34 |
- "exit_status": "0", |
|
38 |
+ "exit_status": 0, |
|
35 | 39 |
"errors": "", |
36 | 40 |
"output": "/home/Huginn" |
37 | 41 |
} |
@@ -41,6 +45,8 @@ module Agents |
||
41 | 45 |
{ |
42 | 46 |
'path' => "/", |
43 | 47 |
'command' => "pwd", |
48 |
+ 'suppress_on_failure' => false, |
|
49 |
+ 'suppress_on_empty_output' => false, |
|
44 | 50 |
'expected_update_period_in_days' => 1 |
45 | 51 |
} |
46 | 52 |
end |
@@ -89,10 +95,19 @@ module Agents |
||
89 | 95 |
|
90 | 96 |
result, errors, exit_status = run_command(path, command, stdin) |
91 | 97 |
|
92 |
- vals = {"command" => command, "path" => path, "exit_status" => exit_status, "errors" => errors, "output" => result} |
|
93 |
- created_event = create_event :payload => vals |
|
98 |
+ payload = { |
|
99 |
+ 'command' => command, |
|
100 |
+ 'path' => path, |
|
101 |
+ 'exit_status' => exit_status, |
|
102 |
+ 'errors' => errors, |
|
103 |
+ 'output' => result, |
|
104 |
+ } |
|
94 | 105 |
|
95 |
- log("Ran '#{command}' under '#{path}'", :outbound_event => created_event, :inbound_event => event) |
|
106 |
+ unless suppress_event?(payload) |
|
107 |
+ created_event = create_event payload: payload |
|
108 |
+ end |
|
109 |
+ |
|
110 |
+ log("Ran '#{command}' under '#{path}'", outbound_event: created_event, inbound_event: event) |
|
96 | 111 |
else |
97 | 112 |
log("Unable to run because insecure agents are not enabled. Edit ENABLE_INSECURE_AGENTS in the Huginn .env configuration.") |
98 | 113 |
end |
@@ -128,5 +143,10 @@ module Agents |
||
128 | 143 |
|
129 | 144 |
[result, errors, exit_status] |
130 | 145 |
end |
146 |
+ |
|
147 |
+ def suppress_event?(payload) |
|
148 |
+ (boolify(interpolated['suppress_on_failure']) && payload['exit_status'].nonzero?) || |
|
149 |
+ (boolify(interpolated['suppress_on_empty_output']) && payload['output'].empty?) |
|
150 |
+ end |
|
131 | 151 |
end |
132 | 152 |
end |
@@ -74,6 +74,8 @@ describe Agents::ShellCommandAgent do |
||
74 | 74 |
describe "#check" do |
75 | 75 |
before do |
76 | 76 |
stub(@checker).run_command(@valid_path, 'pwd', nil) { ["fake pwd output", "", 0] } |
77 |
+ stub(@checker).run_command(@valid_path, 'empty_output', nil) { ["", "", 0] } |
|
78 |
+ stub(@checker).run_command(@valid_path, 'failure', nil) { ["failed", "error message", 1] } |
|
77 | 79 |
end |
78 | 80 |
|
79 | 81 |
it "should create an event when checking" do |
@@ -91,6 +93,34 @@ describe Agents::ShellCommandAgent do |
||
91 | 93 |
expect(Event.last.payload[:errors]).to eq('warning!') |
92 | 94 |
end |
93 | 95 |
|
96 |
+ describe "with suppress_on_empty_output" do |
|
97 |
+ it "should suppress events on empty output" do |
|
98 |
+ @checker.options[:suppress_on_empty_output] = true |
|
99 |
+ @checker.options[:command] = 'empty_output' |
|
100 |
+ expect { @checker.check }.not_to change { Event.count } |
|
101 |
+ end |
|
102 |
+ |
|
103 |
+ it "should not suppress events on non-empty output" do |
|
104 |
+ @checker.options[:suppress_on_empty_output] = true |
|
105 |
+ @checker.options[:command] = 'failure' |
|
106 |
+ expect { @checker.check }.to change { Event.count }.by(1) |
|
107 |
+ end |
|
108 |
+ end |
|
109 |
+ |
|
110 |
+ describe "with suppress_on_failure" do |
|
111 |
+ it "should suppress events on failure" do |
|
112 |
+ @checker.options[:suppress_on_failure] = true |
|
113 |
+ @checker.options[:command] = 'failure' |
|
114 |
+ expect { @checker.check }.not_to change { Event.count } |
|
115 |
+ end |
|
116 |
+ |
|
117 |
+ it "should not suppress events on success" do |
|
118 |
+ @checker.options[:suppress_on_failure] = true |
|
119 |
+ @checker.options[:command] = 'empty_output' |
|
120 |
+ expect { @checker.check }.to change { Event.count }.by(1) |
|
121 |
+ end |
|
122 |
+ end |
|
123 |
+ |
|
94 | 124 |
it "does not run when should_run? is false" do |
95 | 125 |
stub(Agents::ShellCommandAgent).should_run? { false } |
96 | 126 |
expect { @checker.check }.not_to change { Event.count } |